package com.legendshop.openportal.sdk.model.request;

import com.legendshop.openportal.sdk.annotation.EnumValid;
import com.legendshop.openportal.sdk.annotation.JsonValid;
import com.legendshop.openportal.sdk.model.BaseRequest;
import com.legendshop.openportal.sdk.model.constants.AfterSaleReturnGoodsTypeEnum;
import com.legendshop.openportal.sdk.model.constants.AfterSaleServiceTypeEnum;
import com.legendshop.openportal.sdk.model.constants.ApiPathConstants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.AssertFalse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.beans.Transient;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 订单售后提交申请DTO
 */
@Data
@ApiModel(value = "订单售后提交申请DTO")
public class AfterSaleSubmitRequest extends BaseRequest {

    private static final long serialVersionUID = -1605730987181971753L;

    /**
     * 订单号
     */
    @NotNull(message = "订单号不能为空")
    @ApiModelProperty(value = "订单号", required = true)
    private String subNumber;

    /**
     * 第三方申请单号
     */
    @NotNull(message = "第三方申请单号不能为空")
    @Length(max = 30, message = "第三方申请单号要在30字符以内")
    @ApiModelProperty(value = "第三方申请单号", required = true)
    private String thirdApplyNo;

    /**
     * 用户信息
     */
    @NotNull(message = "用户信息不能为空")
    @ApiModelProperty(value = "用户信息", required = true)
    @JsonValid(target = AfsCustomerInfo.class, message = "用户信息格式不正确")
    private String customerInfo;

    /**
     * 申请时取件信息
     */
    @ApiModelProperty(value = "取件信息，请把参数转为json字符串", required = false)
    @JsonValid(target = AfsReturnWareInfo.class, message = "取件信息格式不正确", ignoreEmpty = true)
    private String returnWareInfo;

    /**
     * 售后订单项集合
     */
    @NotBlank(message = "售后订单项集合不能为空")
    @ApiModelProperty(value = "售后订单项集合", required = true)
    @JsonValid(target = OpenAfterSaleApplyItemDTO.class, isArrayJson = true, ignoreEmpty = true)
    private String item;

    @Override
    @ApiModelProperty(hidden = true)
    public String getApiPath() {
        return ApiPathConstants.AFS_APPLY_SUBMIT;
    }

    /**
     * 用户信息
     */
    @Data
    @ApiModel(value = "用户信息")
    public static class AfsCustomerInfo implements Serializable {

        private static final long serialVersionUID = 3254910303609673953L;

        /**
         * 用户联系人
         */
        @NotNull(message = "用户联系人不能为空")
        @ApiModelProperty(value = "用户联系人")
        private String userContactsName;

        /**
         * 用户联系人手机号
         */
        @NotNull(message = "用户联系人手机号不能为空")
        @ApiModelProperty(value = "用户联系人手机号")
        private String userContactsPhone;
    }

    /**
     * 返件信息，商品如何返回客户手中。
     * 当售后类型为换货、维修时，表示商品如何返回客户手中；
     * 当售后类型为退货时，表示退货失败商品返回客户手中的地址。
     */
    @Data
    @ApiModel(value = "返件信息")
    public static class AfsReturnWareInfo implements Serializable {

        private static final long serialVersionUID = 459085675027622495L;

        /**
         * 返货方式 AfterSaleReturnGoodsTypeEnum
         * 售后服务类型存在 退货、维修、换货 时，必传
         */
        @EnumValid(target = AfterSaleReturnGoodsTypeEnum.class, message = "返货方式不正确", ignoreEmpty = true, targetField = "type")
        @ApiModelProperty(value = "返货方式")
        private String returnGoodsType;

        /**
         * 取件省份ID 上门取件必填
         */
        @ApiModelProperty(value = "取件省份ID 上门取件必填")
        private Long fetchProvinceId;

        /**
         * 取件城市ID 上门取件必填
         */
        @ApiModelProperty(value = "取件城市ID 上门取件必填")
        private Long fetchCityId;

        /**
         * 取件县ID 上门取件必填
         */
        @ApiModelProperty(value = "取件县ID 上门取件必填")
        private Long fetchAreaId;

        /**
         * 取件乡镇ID 上门取件必填
         */
        @ApiModelProperty(value = "取件乡镇ID 上门取件必填")
        private Long fetchTownId;

        /**
         * 取件详细地址 上门取件必填
         */
        @ApiModelProperty(value = "取件详细地址 上门取件必填")
        private String fetchDetailedAddress;

        /**
         * 取件预约开始时间 格式 yyyy-MM-dd HH:mm:ss
         */
        @ApiModelProperty(value = "取件预约开始时间 格式 yyyy-MM-dd HH:mm:ss")
        private Date fetchReserveStartTime;

        /**
         * 取件预约结束时间 格式 yyyy-MM-dd HH:mm:ss
         */
        @ApiModelProperty(value = "取件预约结束时间 格式 yyyy-MM-dd HH:mm:ss")
        private Date fetchReserveEndTime;

        /**
         * 检查返货地址
         * @return
         */
        @Transient
        @AssertFalse(message = "返货信息不能为空")
        public Boolean getCheckAfterSaleReturnGoodsAddr() {
            // 如果是上面取件，则取件地址必传
            if (AfterSaleReturnGoodsTypeEnum.CALL_COLLECT.getType().equals(this.returnGoodsType)) {
                return fetchProvinceId == null || fetchCityId == null || fetchAreaId == null || fetchTownId == null;
            }
            return false;
        }
    }

    /**
     * 订单售后提交申请项DTO
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ApiModel(value = "售后服务项")
    public static class OpenAfterSaleApplyItemDTO implements Serializable {

        private static final long serialVersionUID = -5105018617826391988L;

        /**
         * AfterSaleServiceTypeEnum
         */
        @EnumValid(target = AfterSaleServiceTypeEnum.class, message = "售后服务类型不正确")
        @NotNull(message = "售后服务类型不能为空")
        @ApiModelProperty(value = "售后服务类型")
        private Integer serviceType;

        /**
         * 商品ID不能为空
         */
        @NotNull(message = "商品ID不能为空")
        @ApiModelProperty(value = "商品ID")
        protected Long prodId;

        /**
         * 商品规格ID不能为空
         */
        @NotNull(message = "商品规格ID不能为空")
        @ApiModelProperty(value = "商品规格ID")
        protected Long skuId;

        /**
         * 退款金额不能空
         */
        @NotNull(message = "退款金额不能空")
        @ApiModelProperty(value = "退款金额")
        protected BigDecimal refundAmount;

        /**
         * 商品数量
         */
        @ApiModelProperty(value = "商品数量")
        @NotNull(message = "商品数量不能空")
        private Integer num;

        /**
         * 凭证图片集合 多个以逗号分隔 ,
         */
        @ApiModelProperty(value = "凭证图片集合 多个以逗号分隔 ,")
        private String photoFiles;

        /**
         * 售后说明
         */
        @ApiModelProperty(value = "售后说明，300个字之内")
        @Length(max = 300, message = "字数过长")
        private String applyRemark;
    }
}
